requireJS(['constant', 'site', 'handlebars'], function (Constant, site, Handlebars) {
    var url = {
        a: "manager/queryPhoneBookListByPersonalId.do",
        b: "manager/deletePhone.do",
        c: "manager/addPhone.do"

    }

    // 获取url参数
    var personalId = getAurlparam('personalId')
    var pageNum = getAurlparam('pageNum')
    // 传入参数
    var frontData = {
        personalId: personalId,
        pageNum: pageNum
    };
    var init = function () {
        // 查询联系人信息
        site.send({

            url: url.a,
            //async:false
            data: frontData,
            callback: function (data) {
                var list = JSON.parse(data)
                console.log(data)
                if (list.type == '-1') {
                    alert(list.msg)
                }
                // var a=list.content.list;
                // if(a[0]==null){
                //     alert("ccc")
                //     }

                var source = $("#personalPublicList_tpl").html();
                var template = Handlebars.compile(source);

                var html = template(list);
                $('#personalPublicList').html(html);

                // 分页
                $('.product-page-container').createPager({
                    current: frontData.pageNum,
                    total: list.content.total,
                    pageSize: list.content.pageSize,
                    needInput: false,
                    backFn: function (current) {
                        frontData.pageNum = current;
                        init();
                    }
                })
            }
        })
    }
    init();

    // 搜索按钮
    $(".search_phoneNameOrPhone_Btn").on("click", function () {

        frontData.phoneName = $(".search_phoneName").val();
        frontData.phone = $(".search_phone").val();
        init();
    })

    //拿到所有的复选框，当有复选框没选上的时候,取消全选
    $("input:checkbox[name='ch_id']").on('click', function () {
        var checkboxs = $("input:checkbox[name='ch_id']");
        var sum = 0;
        var length = checkboxs.length;
        $.each(checkboxs, function (i, checkbox) {
            if ($(checkbox).prop("checked")) {
                sum++;
            }
        })
        if (sum == length) {
            $('#checkAllOrNot').prop("checked", true);
        } else {
            $('#checkAllOrNot').prop("checked", false);
        }
    })


// 全选和全不选
    $("#checkAllOrNot").on('click', function () {

        $("input[name='ch_id']").prop("checked", $(this).prop("checked"))

    })
// 单个删除
    var ids = {
        ids: 0

    }
    $(".delect_phone").on('click', function () {
        ids.ids = $(this).data('id');
        delect_phone(ids);
    })

    // 删除方法
    function delect_phone(ids) {
        site.send({

            url: url.b,
            //async:false
            data: ids,
            callback: function (data) {
                var list = JSON.parse(data);
                if (list.type == 1) {
                    Constant.fn.message("T", list.msg)
                    pjaxReload();
                }
                else {
                    Constant.fn.message("F", list.msg)
                }
            }

        })
    }

// 批量删除
    $(".batch_delete").on('change', function () {
        var obj = $(this).find("option:selected");
        if (obj.val() == 1) {
            var inp = $("input[name='ch_id']");
            var count = 0;//计数
            for (var i = 0; i < inp.length; i++) {
                if (inp[i].checked) {
                    count++;
                    if (count < 2) {
                        ids.ids = inp[i].value;
                    } else {
                        ids.ids += ("," + inp[i].value);
                    }
                }
            }
            if (count != 0) {
                delect_phone(ids)
            } else {
                alert("未选择")
            }


        }
    })


    //新增联系人弹框

    $(".new_contacts").on('click', function () {
        $(".new_contacts_window").css("display", "block");

    })
    //
    $(".edit_phone").on('click', function () {
        $(".edit_contacts_window").css("display", "block");

    })

    //新增确认

    // new_phone
    $(".new_contacts_commit").click(function () {
        var new_contacts_data = {
            phone: $(".new_phone").val(),
            phoneName: $(".new_phoneName").val(),
            personalId: personalId

        }


        site.send({

            url: url.c,
            //async:false
            data: new_contacts_data,
            callback: function (data) {

                var list = JSON.parse(data);
//alert(list.msg)
                if (list.type == 1) {
                    pjaxReload();
                }
                else {
                    Constant.fn.Message('F', data.msg)
                }
            }
        })
    })


    $(".headerIcon").click(function () {
        $(".new_contacts_window").css("display", "none");
        $(".edit_contacts_window").css("display", "none");

    })
})

